package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.adapter.PayTypeAdapter;
import com.muqi.app.qlearn.adapter.PayTypeAdapter.onPayClick;
import com.muqi.app.qlearn.modles.PayTypeInfo;
import com.muqi.app.qlearn.student.BaseStudentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.wxapi.WXPayEntryActivity;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.widget.MyListView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class BecomeMemberActivity extends BaseStudentActivity implements  onPayClick {
	private MyListView lv;
	private ArrayList<ArrayList<PayTypeInfo>> infos;
	private PayTypeAdapter adapter;
	private String studentId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_become_member);
		registerPayBroadCast();
	}

	private PaySuccessReceiver receiver;

	private void registerPayBroadCast() {
		receiver = new PaySuccessReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(WXPayEntryActivity.PAY_WITH_SUCCESS);
		registerReceiver(receiver, intentFilter);
	}

	private class PaySuccessReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(WXPayEntryActivity.PAY_WITH_SUCCESS)) {
				BecomeMemberActivity.this.finish();
			}
		}
	}

	@Override
	protected void onInit() {
		super.onInit();
		lv = (MyListView) findViewById(R.id.lv);
		infos = new ArrayList<ArrayList<PayTypeInfo>>();
		adapter = new PayTypeAdapter(infos, mContext);
		lv.setAdapter(adapter);
		adapter.setPayClick(this);
	}

	@Override
	protected void onData() {
		super.onData();
		studentId=getIntent().getStringExtra("student_id");
		if(null == studentId || "" .equals(studentId)){
			studentId=mSpUtil.getCurrentChildId();
		}
		MyAsyncHttp.get(mContext, NetWorkApi.GET_PAY_TYPES,
				new AsyncCallBackListener() {

					@Override
					public void onAsyncCallBack(RESULTCODE resuleCode,
							String request, String result) {
						if (resuleCode == RESULTCODE.SUCCESS) {
							Log.e("===", result);
							ArrayList<ArrayList<PayTypeInfo>> tempInfos = ResponseUtils
									.getPayTypeLists(mContext, result);
							if (tempInfos != null) {
								infos.addAll(tempInfos);
								adapter.notifyDataSetChanged();
							}
						}

					}
				});
	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		// TODO Auto-generated method stub

	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	public void onClick(PayTypeInfo onfo) {
		Intent intent = new Intent(this,MoneyChargeActivity.class);
		intent.putExtra("typeInfo", onfo);
		intent.putExtra("student_id", studentId);
		startActivity(intent);
	}
}
